package com.admin.propertistoreadmin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class OlahDataMember extends Activity {

	private ListView listMember;
	private String process;
	private JSONArray jsonArray;
	private JSONObject jsonObject;
	private Streamer streamer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_olah_data_member);
		listMember = (ListView) findViewById(R.id.listMember);
		streamer = new Streamer();
		try {
			getAllData();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void getAllData() {
		new Asyncronous().execute("select");
	}

	private void showData(){

						
			final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map;
			
			for(int i = 0; i < jsonArray.length(); i++){
                JSONObject c = jsonArray.getJSONObject(i);
                
    			map = new HashMap<String, String>();
    			map.put("MemberID", c.getString("MemberID"));
    			map.put("Name", c.getString("Name"));
    			map.put("Tel", c.getString("Tel"));
    			MyArrList.add(map);
    			
			}


	        SimpleAdapter sAdap;
	        sAdap = new SimpleAdapter(OlahDataMember.this, MyArrList, R.layout.activity_column,
	                new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});      
	        listMember.setAdapter(sAdap);
	}

	private void selectAllData() {
		try {
			List<NameValuePair> nvp = new ArrayList<NameValuePair>();
			nvp.add(new BasicNameValuePair("action", "select"));
			jsonArray = streamer
					.postData(VariabelGlobal.url + "/user.php", nvp);
			jsonObject = jsonArray.getJSONObject(0);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private class Asyncronous extends AsyncTask<Object, Integer, Void> {

		@Override
		protected Void doInBackground(Object... arg0) {
			if (arg0[0].equals("select")) {
				process = "select";
				showData();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			try {
				if (process.equals("select")) {
					getAllData();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
